<?php
namespace app\channel\controller;
use think\Db;

class ChannelSon extends Base
{
    //只有CPS一级渠道可以访问
    public function _initialize(){
        parent::_initialize();
        if(!session('admin.cps_id') || session('admin.cps_pid')!=0){
            $this->error('访问错误');
        }
    }

    public function index(){
        if( request()->isAjax() ){
            $page = input('page');
            $limit = input('limit');
            $cpsId = input('cps_id');

            $where['status'] = 1;
            $where['pid'] = session('admin.cps_id');
            !empty($cpsId) && $where['cps_id'] = $cpsId;

            if( ! $count=Db::name('ViewChannel')->where($where)->count() ){
                $this->layTable();
            }

            $select = Db::name('ViewChannel')
                ->where($where)
                ->order('add_time desc')
                ->limit(($page-1)*$limit, $limit)
                ->select();

            $data = [];
            $channelIdName = model('manage/Channel')->channelIdName();
            foreach ($select as $k=>$v){
                $data[] = [
                    'cps_id' => $v['cps_id'],
                    'cps_name' => $channelIdName[ $v['cps_id'] ],
                    'account' => $v['account'],
                    'pay_rate' => $v['pay_rate'] . '%',
                    'login_time' => date('Y-m-d H:i:s', $v['login_time']),
                    'login_ip' => $v['login_ip'],
                    'add_time' => $v['add_time'] ? date('Y-m-d H:i:s', $v['add_time']) : '',
                ];
            }
            $this->layTable(0, '二级渠道列表', $data, $count);
        }else{
            $channelData = model('Base')->channelNotice();
            return view('', ['channel_data'=>$channelData]);
        }
    }

    //添加
    public function add(){
        if( request()->isAjax() ){
            $username = input('username');
            $account = input('account');
            $password = input('password');
            $passwordConfirm = input('password_confirm');
            $payRate = input('pay_rate');
            $intro = input('intro');

            $cps['channel_id'] = 1000;
            $cps['code'] = 'miyu';
            $cps['channel_name'] = 'miyu';
            $cpsId = model('ViewChannel')->where('cps_id', '<', 5000)->max('cps_id');
            $cpsId += 1;
            $cps['cps_id'] = $cpsId;
            $cps['pid'] = session('admin.cps_id');
            $cps['cps_name'] = $username;
            $cps['type'] = 'cps';
            $cps['channel_code'] = $cpsId;
            $cps['intro'] = $intro;
            $cps['status'] = 1;
            $cps['pay_rate'] = $payRate;
            $cps['account'] = $account;
            $cps['password'] = md5($password);
            $cps['password_confirm'] = md5($passwordConfirm);
            $cps['add_time'] = time();

            $addRs = model('Channel')
                ->validate([
                    'cps_name' => 'unique:channel',
                    'account' => 'unique:channel',
                    'password'=>'confirm'
                ], [
                    'cps_name.unique' => '渠道名称已存在',
                    'account.unique' => '登录账号已存在',
                    'password.confirm' => '两次密码不一致'
                ])->allowField(true)->save( $cps );

            $addRs ? $this->layJson(0, '添加成功') : $this->layJson(1, model('Channel')->getError());
        }else{
            return view();
        }
    }

    //编辑
    public function edit(){
        if( request()->isAjax() ){
            $cpsId = input('cps_id');
            $username = input('username');
            $password = input('password');
            $passwordConfirm = input('password_confirm');
            $payRate = input('pay_rate');
            $intro = input('intro');

            $cps['cps_name'] = $username;
            $cps['pay_rate'] = $payRate;
            $cps['intro'] = $intro;
            if(!empty($password)){
                $cps['password'] = md5($password);
                $cps['password']!==md5($passwordConfirm) && $this->layJson(1, '两次密码不一致');
            }

            $whereUser['cps_id'] = ['<>', $cpsId];
            $whereUser['cps_name'] = $username;
            if( model('Channel')->where($whereUser)->count() ){
                $this->layJson(1, '渠道名称已存在');
            }

            $editRs = model('Channel')->allowField(true)->isUpdate(true)->save($cps, ['cps_id'=>$cpsId]);
            $editRs!==false ? $this->layJson(0, '编辑成功') : $this->layJson(1, model('Channel')->getError());
        }else{
            $find = model('Channel')->where('cps_id', input('cps_id'))->find();
            return view('', ['find'=>$find]);
        }
    }

    //删除
    public function del(){
        $delRs = model('Channel')->isUpdate(true)->save(['status'=>-1], ['cps_id'=>input('cps_id')]);
        $delRs!==false ? $this->layJson(0, '删除成功') : $this->layJson(1, '删除失败，请重试');
    }
}
